package com.swallow.auth.application.api.auth;

import com.swallow.auth.api.login.IAuthFacade;
import com.swallow.auth.api.login.dto.CaptchaDTO;
import com.swallow.auth.api.login.dto.RegisterDTO;
import com.swallow.auth.api.login.dto.SendSmsDTO;
import com.swallow.auth.application.convert.auth.CvtAuthMapper;
import com.swallow.auth.common.result.R;
import com.swallow.auth.common.result.Result;
import com.swallow.auth.common.utils.GsonUtil;
import com.swallow.auth.domain.auth.model.AccountEntity;
import com.swallow.auth.domain.auth.model.AuthRoot;
import com.swallow.auth.domain.auth.model.SmsValObj;
import com.swallow.auth.domain.auth.service.IAuthDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 18310740596@163.com
 * @date: 2024-05-11 00:25:21
 * @version: v1.0
 * @describe: 认证相关服务
 */
@Slf4j
@RestController
public class AuthFacadeImpl implements IAuthFacade {
    @Resource
    IAuthDomainService authDomainService;

    @Override
    public Result<Void> smsCaptcha(SendSmsDTO sendSms) {
        log.info("验证码发送::: 请求参数 {}", GsonUtil.toJson(sendSms));

        SmsValObj smsValObj = new SmsValObj();
        smsValObj.setMobile(sendSms.getMobile());
        smsValObj.setSmsType(sendSms.getSendType());

        authDomainService.smsCaptcha(AuthRoot.builder().smsValObj(smsValObj).build());
        log.info("验证码发送::: 短信发送完成.");
        return R.success();
    }

    @Override
    public Result<CaptchaDTO> picCaptcha() {
        AuthRoot authRoot = authDomainService.pictureCaptcha();
        // 转换并返回
        return R.success(CvtAuthMapper.INSTANCE.captchaVal2DTO(authRoot.getCaptchaValObj()));
    }

    @Override
    public Result<String> register(RegisterDTO register) {
        log.info("用户注册::: 用户注册请求参数:{}", GsonUtil.toJson(register));
        AccountEntity entity = CvtAuthMapper.INSTANCE.register2Entity(register);

        String accountNo = authDomainService.register(AuthRoot.builder().accountEntity(entity).build());

        log.info("用户注册::: 用户注册成功, 唯一编号:{}", accountNo);
        return R.success(accountNo);
    }
}
